package top.mschen.blog.demo.feigntest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RefreshScope
public class HelloController {

    @Value("${env}")
    private String env;

    @Value("${label}")
    private String label;

    @RequestMapping("/hello")
    public String index(@RequestParam(value = "name") String name) {

        return "hello "+env+"，this is first messge"+label;
    }

    /**
     *以feign方式的调用
     */
    @GetMapping("/get001/{id}")
    public Domain getDomain01(@PathVariable("id") int id)
    {
        Domain domain = new Domain();
        domain.setId(id);
        return domain;  //service层服务调用
    }
    @GetMapping("/get002/{id}")
    public Domain getDomain02(@PathVariable("id") int id)
    {
         Domain domain = new Domain();
        domain.setId(id);
        return domain;  //service层服务调用
    }

    @GetMapping("/get003")
    public Domain getDomain03(@RequestParam("userName") String userName,@RequestParam("pwd")String pwd,@RequestParam("address")String address)
    {
        Domain domain = new Domain();
//        domain.setId(id);
        domain.setName(userName);
        List list = new ArrayList<String>();
        list.add(pwd);
        list.add(address);
        domain.setStrs(list);
        return domain;//service层服务调用
    }

    @GetMapping("/get004")
    public Domain getDomain04(String userName, String pwd, String address)
    {
        Domain domain = new Domain();
//        domain.setId(id);
        domain.setName(userName);
        List list = new ArrayList<String>();
        list.add(pwd);
        list.add(address);
        domain.setStrs(list);
        return domain;//service层服务调用
    }

    @PostMapping("/post001")
    public Domain postDomain001(@RequestBody Domain domain){
        return domain;//service层服务调用
    }

    @PostMapping("/post002")
    public Domain postDomain002(@RequestBody Domain domain){
        return  domain;
    }

    @PostMapping("/post003")
    public List<Domain> postDomain003(@RequestBody Domain domain){

        List objects = new ArrayList<>();
        objects.add(domain);
        return  objects;
    }

    @PostMapping(value = "/uploadFile")
    public String handleFileUpload(@RequestPart("file") MultipartFile file,@RequestParam("title") String title) {
        return file.getOriginalFilename()+title;
    }


    @PostMapping(value = "/uploadFileMutil", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String handleFileUploadMutil(@RequestPart("file") MultipartFile[] files,@RequestParam("title") String title){
        String name="";
        for (MultipartFile file:files){
            name =name+file.getOriginalFilename();
        }
        return name;
    }
}
